package com.gw.smscheck.controller.sys;

import com.gw.smscheck.entity.sys.LoginUser;
import com.gw.smscheck.entity.sys.VerCode;
import com.gw.smscheck.service.sys.LoginService;
import com.gw.smscheck.util.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 *系统登录接口
 *@author lhq
 *
 */
@Controller
@RequestMapping("/loginController")
@Api(description="【系统登录接口】")
public class LoginController {

@Autowired
private LoginService loginService;

    /**
     * 登陆页面跳转
     * @param req
     * @return
     */
    @RequestMapping("/goLogin")
    public String goLogin(HttpServletRequest req){
        return "login/hemeLogin";
    }

    /**
    * 用户登录check
    *
    */
    @ApiOperation(value="登录验证", notes="登录验证")
    @RequestMapping(value = "loginCheck",  method = { RequestMethod.POST })
    @ResponseBody
    public ResultObj loginCheck(HttpServletRequest request, LoginUser loginUser){
         ResultObj result = loginService.loginCheck(loginUser.getLoginName(),loginUser.getPassword());
         LoginUser resultUser =(LoginUser) result.getData();
         if(result.getCode() == result.CODE_SUCCESS){//如果登陆成功
         //将用户名存入session
             request.getSession().setAttribute("userName",resultUser.getLoginName());
             request.getSession().setAttribute("userId",resultUser.getId());
      
        }
        return result;
    }

    /**
     * 修改密码发送验证码
     * @param phone1
     * @param phone2
     * @return
     */
    @ApiOperation(value="发送验证码", notes="发送验证码")
    @RequestMapping(value = "/setPwdSendSMS",  method = { RequestMethod.POST })
    @ResponseBody
    public ResultObj setPwdSendSMS(@RequestParam(value = "phone1",required=false)String phone1,
                                   @RequestParam(value = "phone2",required=false)String phone2){
        return loginService.setPwdSendSMS(phone1,phone2);
    }

    /**
     * 检查验证码
     * @param verCode
     * @return
     */
    @ApiOperation(value="检查验证码", notes="检查验证码")
    @RequestMapping(value = "/checkVerCode",  method = { RequestMethod.POST })
    @ResponseBody
    public ResultObj checkVerCode(VerCode verCode){
        return loginService.checkVerCode(verCode);
    }

    /**
     * 修改密码
     * @param
     * @return
     */
    @ApiOperation(value="修改密码", notes="修改密码")
    @RequestMapping(value = "/updatePwd",  method = { RequestMethod.POST })
    @ResponseBody
    public ResultObj updatePwd(HttpServletRequest request,@RequestParam(value = "newPwd",required=false)String newPwd,
                            @RequestParam(value = "reNewPwd",required=false)String reNewPwd){
        Integer id = (Integer) request.getSession().getAttribute("userId");
        return loginService.updatePwd(id,newPwd,reNewPwd);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @ApiOperation(value="退出登录", notes="退出登录")
    @RequestMapping(value = "/logout",  method = { RequestMethod.GET })
    @ResponseBody
    public String logout(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession(false);//防止创建Session
        if(session != null){//注销
            session.removeAttribute("userName");
            session.removeAttribute("userId");
//            session.invalidate();
            return ResultObj.successResultObj(null);
        }
        return ResultObj.failResultObj(null);
    }


}
